﻿using Architecture.Repository;
using Architecture.Repository.Interface.Traceability;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traceability.Domain.BreedDomain;
using Traceability.Domain.TraceabilityDto;

namespace Traceability.Domain.Service.TraceabilitiesServices
{
    public class TraceabilitiesService : ITraceabilitiesService
    {
        /// <summary>
        /// 疫苗仓储
        /// </summary>
        protected readonly ITraceabilitiesRepoisotry traceabilitiesRepoisotry;

        /// <summary>
        /// 图片仓储
        /// </summary>
        protected readonly IPictureRepository pictureRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="traceabilitiesRepoisotry">疫苗仓储</param>
        /// <param name="pictureRepository">图片仓储</param>
        public TraceabilitiesService(ITraceabilitiesRepoisotry traceabilitiesRepoisotry, IPictureRepository pictureRepository)
        {
            this.traceabilitiesRepoisotry = traceabilitiesRepoisotry;
            this.pictureRepository = pictureRepository;
        }

        public async Task<int> AddTraceabilities(Traceabilities traceabilities)
        {
            return await traceabilitiesRepoisotry.AddAsync(traceabilities);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="traceabilityId"></param>
        /// <param name="userMame"></param>
        /// <returns></returns>
        public async Task<int> DeleteTraceabilities(int traceabilityId, string userMame)
        {
            var res = await traceabilitiesRepoisotry.GetModelAsync(traceabilityId);
            res.IsDeleted= true;
            res.DeletedBy = userMame;
            res.DeletedDate=DateTime.Now;
            return await traceabilitiesRepoisotry.UpdateAsync(res);
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<TraceabilitiesDto>> GetAllTraceabilitiesList(int shoppingId)
        {
            var list = (from a in await traceabilitiesRepoisotry.GetAllAsync()
                        join b in await pictureRepository.GetAllAsync()
                        on a.TraceabilityId equals b.TraceabilityId
                        where !a.IsDeleted && a.ShoppingId == shoppingId
                        select new TraceabilitiesDto
                        {
                            TraceabilityId = a.TraceabilityId,
                            FeedingDays=a.FeedingDays,
                            ImmuneSystem=a.ImmuneSystem,
                            ImmuneMethods=a.ImmuneMethods,
                            ProductionBatch = a.ProductionBatch,
                            ShoppingId=a.ShoppingId,
                            PictureName = b.PictureName,
                        }).Distinct().ToList();
            return list;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="traceabilities"></param>
        /// <returns></returns>
        public async Task<int> UpdateTraceabilities(Traceabilities traceabilities)
        {
            return await traceabilitiesRepoisotry.UpdateAsync(traceabilities);
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="traceabilityId"></param>
        /// <returns></returns>
        public async Task<Traceabilities> GetTraceabilitiesModel(int traceabilityId)
        {
            return await traceabilitiesRepoisotry.GetModelAsync(traceabilityId);
        }


    }
}
